package com.industry.framework.core.advice;

import com.industry.framework.common.Result;
import com.industry.framework.core.context.CoreApplicationContext;
import com.industry.framework.core.exception.CoreGenericException;
import com.industry.framework.core.exception.CoreInvalidateArgsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author nbZhuozh
 * @Date 2025-03-20  09:54
 **/
@RestControllerAdvice
public class GenericExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GenericExceptionHandler.class);

    //系统类异常
    @ExceptionHandler(CoreGenericException.class)
    public Result<?> handleGenericException(CoreGenericException e) {
        return Result.fail(e.getCode(), e.getMessage());
    }

    //方法参数类异常
    @ExceptionHandler(CoreInvalidateArgsException.class)
    public Result<?> handleInvalidateArgsException(CoreInvalidateArgsException e){
        return Result.fail(e.getCode(), e.getMessage());
    }

    /**
     * 处理其他未捕获异常
     */
    @ExceptionHandler(Exception.class)
    public Result<?> handleGlobalException(Exception e) {
        // 生产环境建议隐藏详细错误，记录日志
        if(!CoreApplicationContext.isProfileActivated("prod")){
            log.error("系统异常: ", e);
        }
        return Result.fail(500, "系统繁忙，请稍后重试");
    }

}
